capture log close
clear all
set maxvar 30000
set more off
pause on
# delimit;

/* 
Figure B15
*/

local data "" ;
local output "" ;

use "prworapersist_bw264_crime_poly1allcov_iv_all.dta", clear ;	

rename n_age18unfav b ;
rename sen_age18unfav se ;
keep variables b se N mean* sd* median* ;

gen temp = substr(variables,1,2) ;
keep if temp == "tm" ;
drop temp ;

gen period2 = substr(variables,-4,4) ;
gen period1 = substr(variables,-9,4) ;
forval i = 1(1)2 { ;
	replace period`i' = "neither" if period`i' == "none" | period`i' == "one_" ;
	replace period`i' = "crime" if period`i' == "crim" ;
	replace period`i' = "crimb" if period`i' == "rimb" | period`i' == "imb_" ;
	replace period`i' = "work" if period`i' == "ork_" ;
} ;

gen temp = substr(variables,3,1) ;
gen amt = 5 if temp == "5" ;
replace amt = 15 if temp == "1" ;
drop temp ;

gen 	temp = substr(variables,-10,1) if period1 != "crimb" & period2 != "crimb" ;
replace temp = substr(variables,-12,1) if period1 == "crimb" & period2 == "crimb" ;
replace temp = substr(variables,-11,1) if (period1 == "crimb" | period2 == "crimb") & temp == "" ;
gen first = 1 if temp == "1" ;
replace first = 2 if temp == "2" ;
drop temp ;

gen type = "" ;
replace type = substr(variables,5,4) if amt == 15 ;
replace type = substr(variables,4,4) if amt == 5 ;

gen empcut = "" ;
replace empcut = substr(variables,9,1) if amt == 15;
replace empcut = substr(variables,8,1) if amt == 5 ;
destring empcut, replace ;

drop variables ;

order type amt first empcut period1 period2 b se mean_rd ;

gen treat_lvl = mean_rd + b ;

gen lo95 = treat_lvl - 1.96*se ;
gen hi95 = treat_lvl + 1.96*se ;

*replace b = b/mean_rd ;
gen abs_b = abs(b) ;
foreach period1 in work crime both neither crimb { ;
	foreach period2 in work crime both neither crimb { ;	
		summ abs_b if period1 == "`period1'" & period2 == "`period2'" ;
		local intens_`period1'_`period2' = r(mean)*100 ;
		
		summ b if period1 == "`period1'" & period2 == "`period2'" ;
		if r(mean) < 0 { ;
			local color_`period1'_`period2' = "red" ;
		} ;
		
		if r(mean) > 0 { ;
			local color_`period1'_`period2' = "green" ;
		} ;
	} ;
} ;

gen gp = period1+"_"+period2 ;
foreach gp in 
	work_work work_crime work_both work_neither work_crimb
	crime_work crime_crime crime_both crime_neither 
	both_work both_crime both_both both_neither 
	neither_work neither_crime neither_both neither_neither neither_crimb
	crimb_crimb crimb_work crimb_neither
	{ ;
    gen byte gp_`gp' = (gp == "`gp'") ;
} ;

foreach type in genr { ;
foreach amt in 5 { ;
foreach first in 1 { ;
foreach empcut in 5 { ;

	preserve ;
	
	keep if type == "`type'" & amt == `amt' & first == `first' & empcut == `empcut' ;
	
	capture drop sort ;
	gen		sort = 20 if gp == "work_work" 	;
	replace	sort = 18 if gp == "work_crimb" ;
	replace	sort = 16 if gp == "work_neither" 	;
	replace	sort = 12 if gp == "crimb_work" ;
	replace	sort = 10 if gp == "crimb_crimb";
	replace	sort = 8  if gp == "crimb_neither" ;
	replace	sort = 4  if gp == "neither_work"  ;
	replace	sort = 2  if gp == "neither_crimb" ;
	replace	sort = 0  if gp == "neither_neither"  ;

	twoway 

		(rbar hi95 lo95	sort 	if gp == "work_work" , color(green*.5) barwidth(.4) horizontal)
		(rbar hi95 lo95	sort 	if gp == "work_crimb", color(green*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if gp == "work_neither" , color(green*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if gp == "crimb_work", color(red*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if gp == "crimb_crimb", color(red*.5) barwidth(.4) horizontal)
		(rbar hi95 lo95	sort 	if gp == "crimb_neither", color(red*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if gp == "neither_work" , color(blue*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if gp == "neither_crimb" , color(blue*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if gp == "neither_neither" , color(blue*.5) barwidth(.4) horizontal)
		
		(pcarrow sort mean_rd sort treat_lvl 	if gp == "work_work" , color(green*1.5) lwidth(medium) msize(medsmall))
		(pcarrow sort mean_rd sort treat_lvl 	if gp == "work_crimb" , color(green) lwidth(medthin) msize(small))
		(pcarrow sort mean_rd sort treat_lvl 	if gp == "work_neither", color(green) lwidth(medthin) msize(small))
		(pcarrow sort mean_rd sort treat_lvl 	if gp == "crimb_work" , color(red) lwidth(medthin) msize(small))
		(pcarrow sort mean_rd sort treat_lvl 	if gp == "crimb_crimb" , color(red*1.5) lwidth(medium) msize(medsmall))
		(pcarrow sort mean_rd sort treat_lvl 	if gp == "crimb_neither" , color(red) lwidth(medthin) msize(small))
		(pcarrow sort mean_rd sort treat_lvl 	if gp == "neither_work" , color(blue) lwidth(medthin) msize(small))
		(pcarrow sort mean_rd sort treat_lvl 	if gp == "neither_crimb" , color(blue) lwidth(medthin) msize(small))
		(pcarrow sort mean_rd sort treat_lvl 	if gp == "neither_neither" , color(blue*1.5) lwidth(medium) msize(medsmall))
		
		(scatter sort mean_rd  	if gp == "work_work" , color(green*1.5) msymbol(X) msize(medlarge))
		(scatter sort mean_rd  	if gp == "work_crimb" , color(green) msymbol(X))
		(scatter sort mean_rd  	if gp == "work_neither", color(green) msymbol(X))
		(scatter sort mean_rd  	if gp == "crimb_work" , color(red) msymbol(X))
		(scatter sort mean_rd  	if gp == "crimb_crimb" , color(red*1.5) msymbol(X) msize(medlarge))
		(scatter sort mean_rd  	if gp == "crimb_neither" , color(red) msymbol(X))
		(scatter sort mean_rd  	if gp == "neither_work" , color(blue) msymbol(X))
		(scatter sort mean_rd  	if gp == "neither_crimb", color(blue) msymbol(X))
		(scatter sort mean_rd  	if gp == "neither_neither", color(blue*1.5) msymbol(X) msize(medlarge))
		
		, 
		legend(off)
		title("Initial and long-term responses", size(medsmall))
		xsc(r(0 .7)) xtitle("Increase in likelihood of initial -> long-term outcome", size(medsmall))
		xlab(0(.1).7)
		ysc(r(-1 21)) 
		ytitle("") 
		ylabel(
		   20 "Work -> work" 18 "Work -> crime" 16 "Work -> neither"
		   12 "Crime -> work" 10 "Crime -> crime" 8 "Crime -> neither" 
		   4 "Neither -> work" 2 "Neither -> crime" 0 "Neither -> neither"
		   , 
		   angle(0) notick labsize(small)) 

		graphregion(color(white))
		bgcolor(white)
		ylabel(,nogrid angle(0))
		plotregion(margin(0 0 0 0))  
		;
		graph export "`output'/figB15.png", width(1000) height(750) replace ;
		restore ;
} ;
} ;
} ;
} ;


